package org.projectwithoutname.handler;
import java.io.IOException;
import java.util.Collections;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.projectwithoutname.ui.pages.MainPage;
import org.projectwithoutname.ui.wizard.NewEpisodeWizard;
import org.projectwithoutname.ui.wizard.NewSerieWizard;


public class NewEpisodeHandler extends AbstractHandler implements IHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		IWorkbenchPage page = window.getActivePage();
		MainPage mainPage = (MainPage) page.findView(MainPage.ID);
		
		NewEpisodeWizard wizard = new NewEpisodeWizard(mainPage.getSelection(), mainPage.root);
		wizard.init(PlatformUI.getWorkbench(), new StructuredSelection());
		WizardDialog dialog = new WizardDialog(wizard.getShell(), wizard);
		int x = dialog.open();
		
		if (x == 0){
			try {
				mainPage.root.eResource().save(Collections.EMPTY_MAP);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		try {
			Thread.sleep(1500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
